package com.zlfund.cat.zjgsmk.ui.channel;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.zlfund.cat.zjgsmk.R;
import com.zlfund.cat.zjgsmk.adapter.AdapterFactory;
import com.zlfund.cat.zjgsmk.adapter.RecyclerViewAdapter;
import com.zlfund.cat.zjgsmk.adapter.RecyclerViewHolder;
import com.zlfund.cat.zjgsmk.adapter.RecyclerViewOneTypeAdapterHelper;
import com.zlfund.cat.zjgsmk.adapter.Type;
import com.zlfund.cat.zjgsmk.bean.SocialBean;
import com.zlfund.cat.zjgsmk.net.INetCallback;
import com.zlfund.cat.zjgsmk.ui.ActivitySwitch;
import com.zlfund.cat.zjgsmk.ui.base.BaseActivity;
import com.zlfund.cat.zjgsmk.util.DateUtil;
import com.zlfund.cat.zjgsmk.util.GlideUtil;

import java.util.Date;

import butterknife.BindView;

public class SocialActivity extends BaseActivity {

    @BindView(R.id.rv_dynamics)
    RecyclerView mRvDynamics;
    @BindView(R.id.btn_submit)
    Button mBtnSubmit;
    private RecyclerViewAdapter<SocialBean.RBean> mAdapter;
    private int mPage = 0;
    private String mChannelId;
    private boolean mIsLoading;
    private boolean mHasMore;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_social);
    }

    @Override
    protected void setListener() {
        mBtnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(mContext, NewDynamicActivity.class));
            }
        });
    }

    @Override
    protected void initData() {
        mAdapter = AdapterFactory.singleTon().createOneTypeRvAdapter(mContext, new RecyclerViewOneTypeAdapterHelper<SocialBean.RBean>(R.layout.item_social) {
            @Override
            public void initViews(RecyclerViewHolder holder, int position, SocialBean.RBean rBean) {
                if (holder.getType() == Type.TYPE0) {
                    holder.getView(R.id.tv_name, TextView.class).setText(rBean.getNamed());
                    GlideUtil.loadUrl(mContext, rBean.getIco(), holder.getView(R.id.iv_icon, ImageView.class));
                    ImageView ivContent = holder.getView(R.id.iv_content, ImageView.class);
                    if (rBean.getFs() != null && !rBean.getFs().isEmpty()) {
                        ivContent.setVisibility(View.VISIBLE);
                        SocialBean.RBean.FsBean fsBean = rBean.getFs().get(0);
                        GlideUtil.loadUrl(mContext, TextUtils.isEmpty(fsBean.getIco()) ? fsBean.getFiles() : fsBean.getIco(), ivContent);
                    } else {
                        ivContent.setVisibility(View.GONE);
                    }
                    TextView tvTitle = holder.getView(R.id.tv_title, TextView.class);
                    if (rBean.getTitle() != null) {
                        tvTitle.setVisibility(View.VISIBLE);
                        tvTitle.setText(rBean.getTitle());
                    } else {
                        tvTitle.setVisibility(View.GONE);
                    }
                    holder.getView(R.id.tv_content, TextView.class).setText(rBean.getContent());
                    holder.getView(R.id.tv_time, TextView.class).setText(DateUtil.parseDate(new Date(rBean.getTimes())));
                    holder.getView(R.id.tv_from, TextView.class).setText(rBean.getLabel());
                    holder.getView(R.id.tv_praise_num, TextView.class).setText(String.valueOf(rBean.getPraise_sum()));
                    holder.getView(R.id.tv_comment_num, TextView.class).setText(String.valueOf(rBean.getComment_sum()));
                    holder.getView(R.id.tv_location, TextView.class).setText(rBean.getAddr() == null ? "未知" : rBean.getAddr());
                }
            }
        });

        mRvDynamics.setLayoutManager(new LinearLayoutManager(mContext));
        mRvDynamics.setAdapter(mAdapter);
        mChannelId = mIntent.getStringExtra(ActivitySwitch.CHANNELID);
        loadData();
    }

    private void loadData() {
        NetUtil.getChannelData(mPage, "4", mChannelId, new INetCallback<SocialBean>() {
            @Override
            public void response(SocialBean socialBean) {
                mPage++;
                if (socialBean.getR().size() != 20) {
                    mHasMore = false;
                }
                mAdapter.addMoreList(socialBean.getR(), true, R.layout.load_more);
            }

            @Override
            public void error(String str) {
                mHasMore = false;
                mIsLoading = false;
                mAdapter.clearLoadMore();
            }
        });
    }

}
